using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class UserPostUploadVoteDal : Base
    {
        public enum Fields
        {
            Id,
            UserId,
            UserPostUploadId,
            Title,
            PostId,
            Score,
            Created
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.UserId, reader, table);
            GetReaderValue<int>(Fields.UserPostUploadId, reader, table);
            GetReaderValue<string>(Fields.Title, reader, table);
            GetReaderValue<int>(Fields.PostId, reader, table);
            GetReaderValue<decimal>(Fields.Score, reader, table);
            GetReaderValue<DateTime>(Fields.Created, reader, table);
        }

        public void Update(ref int id, int userId, int userPostUploadId, ref string title, ref int postId, decimal score, DateTime created, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "UserPostUploadId", userPostUploadId, SqlDbType.Int);
            AddInputParameter(cmd, "Score", score, SqlDbType.Decimal, null, 5, 4);
            AddInputParameter(cmd, "Created", created, SqlDbType.DateTime);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "TitleOut", SqlDbType.VarChar, 500);
            AddOutputParameter(cmd, "PostIdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            title = GetOutputValue<string>(cmd, "TitleOut");
            postId = GetOutputValue<int>(cmd, "PostIdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyId");

            
        }

        public Hashtable GetByUserIdAndUploadId(int userId, int userPostUploadId)
        {
            SqlCommand cmd = CreateCommand("GetByUserIdAndUploadId");
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "UserPostUploadId", userPostUploadId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllByUserIdAndPostId(int userId, int postId)
        {
            SqlCommand cmd = CreateCommand("GetAllByUserIdAndPostId");
            AddInputParameter(cmd, "UserId", userId, SqlDbType.Int);
            AddInputParameter(cmd, "PostId", postId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }
    }
}
